package com.dingpeikeji.quickdeer.core.modules.ordersetdtl.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.modules.ordersetdtl.entity.TOrdersSetdtl;
import com.dingpeikeji.quickdeer.core.modules.ordersetdtl.service.TOrdersSetdtlService;

/**
 * 结算订单Controller
 * @author mash
 * @version 2017-03-30
 */
@Controller
@RequestMapping(value = "${adminPath}/ordersetdtl/tOrdersSetdtl")
public class TOrdersSetdtlController extends BaseController {

	@Autowired
	private TOrdersSetdtlService tOrdersSetdtlService;
	
	@ModelAttribute
	public TOrdersSetdtl get(@RequestParam(required=false) String id) {
		TOrdersSetdtl entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tOrdersSetdtlService.get(id);
		}
		if (entity == null){
			entity = new TOrdersSetdtl();
		}
		return entity;
	}
	
	@RequiresPermissions("ordersetdtl:tOrdersSetdtl:view")
	@RequestMapping(value = {"list", ""})
	public String list(TOrdersSetdtl tOrdersSetdtl, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TOrdersSetdtl> page = tOrdersSetdtlService.findPage(new Page<TOrdersSetdtl>(request, response), tOrdersSetdtl); 
		model.addAttribute("page", page);
		return "modules/ordersetdtl/tOrdersSetdtlList";
	}

	@RequiresPermissions("ordersetdtl:tOrdersSetdtl:view")
	@RequestMapping(value = "form")
	public String form(TOrdersSetdtl tOrdersSetdtl, Model model) {
		model.addAttribute("tOrdersSetdtl", tOrdersSetdtl);
		return "modules/ordersetdtl/tOrdersSetdtlForm";
	}

	@RequiresPermissions("ordersetdtl:tOrdersSetdtl:edit")
	@RequestMapping(value = "save")
	public String save(TOrdersSetdtl tOrdersSetdtl, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tOrdersSetdtl)){
			return form(tOrdersSetdtl, model);
		}
		tOrdersSetdtlService.save(tOrdersSetdtl);
		addMessage(redirectAttributes, "保存结算订单成功");
		return "redirect:"+Global.getAdminPath()+"/ordersetdtl/tOrdersSetdtl/?repage";
	}
	
	@RequiresPermissions("ordersetdtl:tOrdersSetdtl:edit")
	@RequestMapping(value = "delete")
	public String delete(TOrdersSetdtl tOrdersSetdtl, RedirectAttributes redirectAttributes) {
		tOrdersSetdtlService.delete(tOrdersSetdtl);
		addMessage(redirectAttributes, "删除结算订单成功");
		return "redirect:"+Global.getAdminPath()+"/ordersetdtl/tOrdersSetdtl/?repage";
	}

}